package com.lds.xiaohashu.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @program: xiaohashu
 * @description:
 * @author: lds
 * @create: 2024-11-08 09:11
 **/
@Configuration
public class ThreadPoolConfig {

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数
        executor.setCorePoolSize(10);
        //最大线程数
        executor.setMaxPoolSize(50);
        //队列容量
        executor.setQueueCapacity(200);
        //线程活跃时间（秒）
        executor.setKeepAliveSeconds(30);
        //线程前缀
        executor.setThreadNamePrefix("AuthExecutor-");

        //拒绝策略:由调用线程处理（一般为主线程）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        //等待所有任务结束后关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //设置等待时间，如果超过时间 没有销毁强制销毁确保应用最后关闭
        executor.setAwaitTerminationSeconds(60);

        executor.initialize();
        return executor;
    }
}